package com.example.littleken.ormlite_text.Activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.littleken.ormlite_text.R;
import com.example.littleken.ormlite_text.Utils.Utils;
import com.example.littleken.ormlite_text.bean.LifeDiary;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;

/**
 * 日记添加Activity
 */
public class lifediaryAddActivity extends AppCompatActivity  implements View.OnClickListener{
    private Button btn_addforadd;
    private Button btn_deleteforadd;
    private EditText et_lifediary;
    private EditText et_title;

    //数据库连接
    public Dao   lifediaryDAO = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lifediary_add);
        //初始化数据库连接
        lifediaryDAO= Utils.initDb(lifediaryAddActivity.this);
        initView();


    }

    private void initView() {
        btn_addforadd = (Button) findViewById(R.id.btn_addforadd);
        btn_deleteforadd = (Button) findViewById(R.id.btn_deleteforadd);
        et_title = (EditText) findViewById(R.id.et_title);
        et_lifediary = (EditText) findViewById(R.id.et_lifediary);

        btn_addforadd.setOnClickListener(this);
        btn_deleteforadd.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_addforadd:
                install();//插入日记
                break;
            case R.id.btn_deleteforadd:
                etClear();//清空
                break;
        }
    }
    //用于保存用户输入的信息
    private String title = null;
    private String lifediary = null;

    /**
     * 用于插入日记
     */
    private void install() {
        try {
            title = et_title.getText().toString().trim();
            lifediary = et_lifediary.getText().toString().trim();
            //内容判空
            if(TextUtils.isEmpty(title) && TextUtils.isEmpty(lifediary)){
                Toast.makeText(lifediaryAddActivity.this,(String) this.getResources().getText(R.string.asklifediary),Toast.LENGTH_SHORT).show();
                return;
            }
            if(TextUtils.isEmpty(lifediary)){
                Toast.makeText(lifediaryAddActivity.this,(String) this.getResources().getText(R.string.lifediaryisenpty),Toast.LENGTH_SHORT).show();
                return;
            }

            LifeDiary lifeDiary = new LifeDiary(title,lifediary,Utils.getNowTime());//生成日记实体类
            lifediaryDAO.create(lifeDiary);//将日记插入数据库
           //返回插入成功消息
            lifediaryAddActivity.this.setResult(RESULT_OK);
            //跳转回MainActivity
            lifediaryAddActivity.this.finish();
        } catch (SQLException e) {
            this.setResult(RESULT_CANCELED);
            e.printStackTrace();
        }

    }

    /**
     * 清空所输入的信息
     */
    private void etClear(){
        et_title.setText("");
        et_title.setHint(this.getResources().getText(R.string.asktitle));
        et_lifediary.setText("");
        et_lifediary.setHint(this.getResources().getText(R.string.asklifediary));
    }

}
